package com.xinsoft.entity.dto;

import cn.hutool.core.util.ObjectUtil;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.utils.TokenUtil;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.UUID;

/**
 * 出入库传参
 */
@Data
public class ErpInOrOutStockParam {

    /**
     * 物料ID
     */
    @NotNull(message = "物料ID不能为空")
    private Integer materialId;

    /**
     * 数量
     */
    // @NotNull(message = "数量不能为空")
    @Positive(message = "数量必须大于0")
    private BigDecimal qty;

    /**
     * 库存数量（盘点时用）
     */
    private BigDecimal invQty;

    /**
     * 库存批次
     */
    @NotBlank(message = "批次不能为空")
    private String batch;

    /**
     * 仓库ID
     */
    @NotNull(message = "仓库ID不能为空")
    private Integer warehouseId;

    /**
     * 库位ID
     */
    @NotNull(message = "库位ID不能为空")
    private Integer warehItemId;

    /**
     * 源单id
     */
    private Integer sourceId;

    /**
     * 源单明细id
     */
    private Integer sourceItemId;

    /**
     * 源单编码
     */
    private String sourceCode;
    /**
     * 供应商
     */
    private String supplyId;


    /**
     * 客户--销售出库需要该参数
     */
    private String custId;

    /**
     * 调入仓库ID
     */
    private Integer moveInWarehouseId;

    /**
     * 调入库位ID
     */
    private Integer moveInWarehouseItemId;

    public static KisStockInOrOutDto voToPo(ErpInOrOutStockParam param) {
        if (param == null) {
            return null;
        }
        KisStockInOrOutDto po = new KisStockInOrOutDto();
        UUID uuid = UUID.randomUUID();
        po.setGUID(uuid.toString());
        po.setSManagerId(TokenUtil.getUserId().toString());
        po.setFManagerId(TokenUtil.getUserId().toString());
        po.setUserId(TokenUtil.getUserId().toString());
        Integer clerkId = TokenUtil.getUser().getClerkId();// 金蝶用户id为空，对接到金蝶的单据将缺少制单人字段->单据无法删除
        if (ObjectUtil.isEmpty(clerkId)){
            throw new BDException("当前用户无法进行当前操作！");
        }
        po.setBillerId(clerkId.toString());
        po.setSupplyId(param.getCustId());
        po.setBATCHNO(param.getBatch());
        po.setItemId(param.getMaterialId().toString());
        po.setQty(param.getQty().toString());
        po.setSecQty(param.getQty().toString());
        if (Objects.isNull(param.getMoveInWarehouseId())) {
            po.setDCStockId(param.getWarehouseId().toString());
            po.setDCSPId(Objects.isNull(param.getWarehItemId()) ? "" : param.getWarehItemId().toString());
        } else {
            po.setDCStockId(param.getMoveInWarehouseId().toString());
            po.setDCSPId(Objects.isNull(param.getMoveInWarehouseItemId()) ? "" : param.getMoveInWarehouseItemId().toString());
            po.setSCStockId(param.getWarehouseId().toString());
            po.setSCSPId(Objects.isNull(param.getWarehItemId()) ? "" : param.getWarehItemId().toString());
        }
        po.setSrcInterId(Objects.isNull(param.getSourceId()) ? "" : param.getSourceId().toString());
        po.setSrcEntryId(Objects.isNull(param.getSourceItemId()) ? "" : param.getSourceItemId().toString());
        po.setFOrderBillNo(param.getSourceCode());
        po.setSrcBillNo(param.getSourceCode());
        po.setInvQty(Objects.isNull(param.getInvQty()) ? "" : param.getInvQty().toString());
        po.setInvSecQty(Objects.isNull(param.getInvQty()) ? "" : param.getInvQty().toString());
        if (Objects.nonNull(param.getSupplyId())) po.setSupplyId(param.getSupplyId());
        return po;
    }

}
